import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from page.login_page import LoginPage
from common.base import Base
from common.functions import open_browser



class AddressPage(Base):
    # 定位'用户中心'并点击
    def click_user(self):
        self.base_click((By.LINK_TEXT, '用户中心'))

    # 定位'收货地址'并点击 必填
    def click_address(self):
        self.base_click((By.XPATH, '//div[@class="userMenu"]/a[4]'))

    # 定位省份  必填
    def select_province(self, index):
        self.base_select_text((By.ID, 'selProvinces_0'),index)

    # 定位市 必填
    def select_city(self, index):
        self.base_select_text((By.ID, 'selCities_0'),index)

    # 定位区 必填
    def select_area(self, index):
        self.base_select_text((By.ID, 'selDistricts_0'),index)

    # 定位收货人姓名和输入 必填
    def input_name(self, cons_name):
        self.base_send_keys((By.ID, 'consignee_0'), cons_name)

    # 定位电子邮件地址 必填
    def input_email(self,add):
        # 清空
        self.base_clear((By.ID, 'email_0'))
        # 再输入
        self.base_send_keys((By.ID,'email_0'),add)

    # 定位详细地址和输入 必填
    def input_address(self, address):
        self.base_send_keys((By.ID, 'address_0'),address)

    # 定位邮政编码和输入
    def input_zipcode(self, zipcode):
        self.base_send_keys((By.ID, 'zipcode_0'),zipcode)

    # 定位电话和输入  (手机和电话选一)
    def input_tel(self, tel):
        self.base_send_keys((By.ID, 'tel_0'), tel)

    # 定位手机和输入
    def input_mobile(self, mobile):
        self.base_send_keys((By.ID, 'mobile_0'), mobile)

    # 定位确认新增收货地址
    def click_add(self):
        self.base_click((By.NAME, 'submit'))

    # 定位确认修改和点击
    def click_alter(self):
        self.base_click((By.XPATH, '//input[@value="确认修改"]'))

    # 定位删除和点击
    def click_del(self):
        self.base_click((By.NAME, 'button'))

    # 弹窗点击取消
    def alert_dismiss(self):
        alert = self.driver.switch_to.alert
        alert.dismiss()
        time.sleep(3)

    #  弹窗确定
    def alert_accept(self):
        alert = self.driver.switch_to.alert
        alert.accept()
        time.sleep(2)

    # TODO 新增收获地址
    # 定位省份和选择 必填
    def select_province2(self, index):
        self.base_select_text((By.ID, 'selProvinces_1'),index)

    # 定位市和选择 必填
    def select_city2(self,index):
        self.base_select_text((By.ID, 'selCities_1'),index)

    # 定位区和选择 必填
    def select_area2(self, index):
        self.base_select_text((By.ID, 'selDistricts_1'),index)

    # 定位收货人姓名和输入 必填
    def input_name2(self, name):
        self.base_find_element((By.ID, 'consignee_1')).send_keys(name)

    # 定位电子邮件地址和输入 必填
    def input_email2(self, email):
        # 清空
        self.base_clear((By.ID, 'email_1'))
        # 再输入
        self.base_send_keys((By.ID,'email_1'),email)

    # 定位详细地址和输入 必填
    def input_address2(self, address):
        self.base_find_element((By.ID, 'address_1')).send_keys(address)

    # 定位邮政编码和输入
    def input_zipcode2(self, zipcode):
        self.base_send_keys((By.ID, 'zipcode_1'),zipcode)

    # 定位电话和输入
    def input_tel2(self, tel):
        self.base_send_keys((By.ID, 'tel_1'),tel)

    # 定位手机和输入
    def input_mobile2(self, mobile):
        self.base_send_keys((By.ID, 'mobile_1'),mobile)

    # 定位确认新增收货地址
    def click_add2(self):
        self.base_click((By.CLASS_NAME, 'bnt_blue_2'))


if __name__ == '__main__':
    driver = open_browser()
    # login = LoginPage(driver)
    # addr = AddressPage(driver)
    # login.input_username('lym')
    # login.input_password('123456')
    # login.btn_login()